package com.kunda.netty.marshalling;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqServerHandler extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		SubscribeReq req = (SubscribeReq)msg;
		if("helloworld".equalsIgnoreCase(req.getUserName())){
			System.out.println("server accept client subscribe req:[" + req+"]");
			ctx.writeAndFlush(resp(req.getSubReqId()));
		}
	}
	
	private SubscribeResp resp(int reqId){
		SubscribeResp resp = new SubscribeResp();
		resp.setSubReqId(reqId);
		resp.setRespCode(0);
		resp.setDesc("Netty book order success");
		return resp;
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
	
	
}
